package com.atguigu.runmain;

import com.atguigu.rulepo.CutTenRule;
import com.atguigu.rulepo.TenPercentDiscountAction;
import com.atguigu.rulepo.TenPercentDiscountCondition;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngineParameters;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.core.RuleBuilder;
import org.jeasy.rules.support.composite.ActivationRuleGroup;

public class PriorityOrClient {
    //同时发生，只执行一个
    public static void main(String[] args) {
        // 创建执行器参数
        RulesEngineParameters parameters = new RulesEngineParameters().skipOnFirstAppliedRule(true);
        // 创建执行期引擎
        DefaultRulesEngine discountEngine = new DefaultRulesEngine(parameters);
        // 链式规则完成接口规则的定义
        Rule tenPercentRule = new RuleBuilder().name("200~400的折扣")
                .description("200~400的折扣")
                .when(new TenPercentDiscountCondition())
                .then(new TenPercentDiscountAction())
                .build();
        // 注解实现方式
        CutTenRule cutTenRule = new CutTenRule();

        ActivationRuleGroup unitRuleGroup = new ActivationRuleGroup("单元规则", "单元规则");
        unitRuleGroup.addRule(cutTenRule);
        unitRuleGroup.addRule(tenPercentRule);
        // 添加规则
        Rules rules = new Rules();
        rules.register(unitRuleGroup);
        // 定义事实
        Facts facts = new Facts();
        for (int i = 190; i < 220; i += 10) {
            // 添加事实
            facts.put("price", i);
            // 触发引擎
            discountEngine.fire(rules, facts);
        }
    }
}
